package day8;

import java.util.Scanner;

import org.junit.Test;

public class Train {
	
	
	
	//实现trim方法一
	@Test
	public void test1() {
		String str = "";
		char[] array = str.toCharArray();
	 	for(int i = 0; i < array.length; i++) {
			if(array[i] == ' ') {
			   continue;
			}
			System.out.println(i);
			for(int j = array.length - 1; j >= 0; j--) {
				if(array[j] == ' ') {
					continue;
			    }
				else {
					System.out.println(str.substring(i, j + 1));
					break;
				}
            }
			break;
		 }
	}
	
	
	//
	@Test
	public void test2() {
		String str = "I Love Java !";
		char[] array = str.toCharArray();
		for(int i = 0; i < array.length; i++) {
			if (array[i] >= 'A' && array[i] <= 'Z') {
				//可以用array[i] = (char) (array[i] + 32);
				//不能用array[i] = array[i] + 32
                array[i] += 32;
			}
			
		}
		//替换时需要建立新的数组赋值
		String str2 = new String(array);
		System.out.println(str2);

	}
	
	@Test
	public void test3() {
		String str = "I Love Java !";
		char[] array = str.toCharArray();
		System.out.println(str.toLowerCase());
		System.out.println(str.toUpperCase());
    }
	@Test
	public void test4() {
		String str = "this is my homework! I must finish it!";
		String[] array = str.split(" ");
		for (String string : array) {
			System.out.println(string);
		}
		System.out.println(array.length);
		

		
	}
	
	//实现trim方法二
	@Test
	public void test5() {
		String str = " a";
		char[] array = str.toCharArray();
		System.out.println(array.length);
		int start = 0;
		int end = array.length - 1;
		for (int i = 0; i < array.length; i++) {
			if(array[i] == ' ') {
				start++;
				
			}
			else {
				break;
			}
			
			
		}
		System.out.println(start);//4
		for (int i = array.length- 1; i >= 0;i--) {
			if(array[i] == ' ') {
				end--;
				
			}
			else {
				break;
			}
			
		}
		System.out.println(end);
		
		System.out.println(str.substring(start, end + 1));
	}
	
	//实现trim方法三
	@Test
	public void test6() {
		String str = "";
		char[] array = str.toCharArray();
		Integer start = 0;
		Integer end = array.length - 1;//str.length()
		while(start <=end && str.charAt(start) == '-') {
			start++;
		}
		while(start <=end && str.charAt(end) == '-') {
			end--;
		}
		System.out.println(str.substring(start, end + 1));
	}
	@Test
	public void test7() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个字符串");
		String str = scanner.next();
		char[] array = str.toCharArray();
		boolean isHuiWen = true;
		for (int i = 0; i < array.length / 2; i++) {
			if(array[i] != array[array.length - 1]) {
				isHuiWen = false;
				
			}
			
		}
		
		if(isHuiWen == true) {
			System.out.println("是回文");
		}
		else {
			System.out.println("不是回文");
		}
	}
	
	
	@Test
	public void test8() {
		for (int i = 1; i <= 9; i++) {
			for(int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i  + "=" + i * j + "\t");
			}
			System.out.println();
			
		}
		
	}
	
	
	
	// 求出上面数组中0-9分别出现的次数（有一点难度）

    // int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
	//用双重for循环
	@Test
	public void test9() {
		int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
		for (int i = 0; i <= 9; i++) {
			int sum = 0;
			for(int j = 0; j < scores.length; j++) {
				if (scores[j] == i) {
					sum++;
				}
				
			}
			System.out.println(i + "出现的次数为：" + sum);
		}
	}
	@Test
	public void test10() {
		String goodsName = "诺基亚原装5800耳机";
		String name = "HTC" + goodsName.substring(3);
		System.out.println(name);
	}



}
	
